/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.validator.internal;

import java.util.*;
import java.util.regex.*;
import ch.trackedbean.validator.*;
import ch.trackedbean.validator.defaultValidators.*;

/**
 * Interpolator for {@link AbstractValidator}s messages.<br>
 * If you want to override the default messages then create your own resource bundle in <code>META-INF/services</code> using the
 * <code>ch.trackedbean.validator.messages</code> as base name.
 * 
 * @author M. Hautle
 */
public class DefaultInterpolator implements IMessageInterpolator {
    /** The location of the default message properties file. */
    private static final String DEFAULT_PROPERTIES = "ch.trackedbean.validator.internal.resource.messages";

    /** The location of the user message properties file. */
    private static final String USER_PROPERTIES = "META-INF/ch.trackedbean.validator.messages";

    /** An empty resource bundle. */
    private static final ResourceBundle EMPTY_BUNDLE = new ResourceBundle() {
        @Override
        public Enumeration<String> getKeys() {
            return null;
        }

        @Override
        protected Object handleGetObject(String key) {
            return null;
        }
    };

    /** The resource bundle for the default messages. */
    private final ResourceBundle defaultMsg;

    /** The resource bundle for the user messages. */
    private final ResourceBundle userMsg;

    /** Pattern to use for detetcting the place holders. */
    private final Pattern placeHolder = Pattern.compile("\\$\\{(\\d+?)\\}");

    /**
     * Default constructor.
     */
    public DefaultInterpolator() {
        defaultMsg = ResourceBundle.getBundle(DEFAULT_PROPERTIES);
        ResourceBundle user = EMPTY_BUNDLE;
        try {
            user = ResourceBundle.getBundle(USER_PROPERTIES);
        } catch (MissingResourceException e) {
        }
        userMsg = user;
    }

    /**
     * Default constructor.
     * 
     * @param resourceFile The resource bundle base file name
     */
    public DefaultInterpolator(String resourceFile) {
        defaultMsg = ResourceBundle.getBundle(DEFAULT_PROPERTIES);
        userMsg = ResourceBundle.getBundle(resourceFile);
    }

    /**
     * {@inheritDoc}
     */
    public String getMessage(String key) {
        try {
            return userMsg.getString(key);
        } catch (MissingResourceException e) {
            // do a lookup in the default message bundle if the user lookup failed
            return defaultMsg.getString(key);
        }
    }

    /**
     * {@inheritDoc}
     */
    public String getMessage(String key, Object[] values) {
        final String msg = getMessage(key);
        final Matcher m = placeHolder.matcher(msg);
        final StringBuilder str = new StringBuilder();
        int start = 0;
        while (m.find()) {
            str.append(msg.substring(start, m.start()));
            str.append(values[Integer.parseInt(m.group(1))]);
            start = m.end();
        }
        str.append(msg.substring(start));
        return str.toString();
    }
}
